require 'builder'

module UnfuddleMyEmail
  # Create an XML formatted ticket.
  #
  # Expects an object that responds to the following methods:
  #
  # * +subject+: The summary for the ticket
  # * +from+: An array of authors
  # * +body+: Will be added the the ticket description
  class EmailTicket
    attr_accessor :message

    def initialize(message)
      @message = message
    end


    # Returns an XML representation of an Unfuddle ticket.
    def to_xml
      builder = Builder::XmlMarkup.new
      return xml = builder.ticket { |ticket|
        ticket.priority    "1"
        ticket.summary     subject
        ticket.description description
      }
    end

    private

    def subject
      @message.subject
    end

    def description
      "From: #{from}\n\n#{body}"
    end

    def from
      @message.from.join(',')
    end

    def body
      @message.body
    end
  end
end
